package com.mathofmattix.books;

import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import tabbedpane.ClosableTabbedPane;

public class CatalogUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel searchPanel = new JPanel();
	private JTextField searchField, isbnField, authorField, titleField,
			releaseField, hardcoverField, copiesField;
	private JRadioButton searchByIsbn = new JRadioButton("ISBN Search"),
			searchByTitle = new JRadioButton("Title Search"),
			searchByAuthor = new JRadioButton("Author Search"),
			searchByReleaseDate = new JRadioButton("Release Date Search");
	private JButton searchSubmit, addSubmit;
	private JRadioButton[] searches = { searchByIsbn, searchByTitle,
			searchByAuthor, searchByReleaseDate };
	private String[] columnNames = { "ISBN", "Title", "Author", "Release Date",
			"Hardcover", "Copies" };
	private Catalog germantownCatalog = new Catalog("Germantown");
	private JPanel addPanel = new JPanel();
	private static CatalogUI root;
	private JLabel isbnLabel, titleLabel, authorLabel, releaseLabel,
			hardcoverLabel, copiesLabel;
	private ClosableTabbedPane tabbedPane = new ClosableTabbedPane();

	public CatalogUI() {
		super("Germantown Catalog");

		searchByTitle.setSelected(true);

		searchPanel.setLayout(new FlowLayout());

		searchSubmit = new JButton("Submit");
		addSubmit = new JButton("Submit");
		isbnLabel = new JLabel("ISBN number:");
		titleLabel = new JLabel("Title:");
		copiesLabel = new JLabel("How many copies are there?");
		authorLabel = new JLabel("Author:");
		releaseLabel = new JLabel("Year Released");
		hardcoverLabel = new JLabel("Is it Hardcover?");

		searchField = new JTextField(20);
		isbnField = new JTextField(13);
		titleField = new JTextField(20);
		authorField = new JTextField(20);
		releaseField = new JTextField(20);
		hardcoverField = new JTextField(20);
		copiesField = new JTextField(20);

		JPanel buttonPanel = new JPanel();

		ButtonGroup searchesGroup = new ButtonGroup();
		searchPanel.add(searchField);
		searchPanel.add(searchSubmit);
		for (JRadioButton search : searches) {
			searchesGroup.add(search);
			buttonPanel.add(search);
		}
		searchPanel.add(buttonPanel);

		JPanel submitPanel = new JPanel();
		submitPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		submitPanel.add(addSubmit);

		addPanel.setLayout(new GridLayout(0, 2, 20, 5));

		addPanel.add(isbnLabel);
		addPanel.add(isbnField);
		addPanel.add(titleLabel);
		addPanel.add(titleField);
		addPanel.add(authorLabel);
		addPanel.add(authorField);
		addPanel.add(releaseLabel);
		addPanel.add(releaseField);
		addPanel.add(hardcoverLabel);
		addPanel.add(hardcoverField);
		addPanel.add(copiesLabel);
		addPanel.add(copiesField);
		addPanel.add(submitPanel);

		ListenerHandler handler = new ListenerHandler();
		searchSubmit.addActionListener(handler);
		addSubmit.addActionListener(handler);
		tabbedPane.addTab("Search", searchPanel);
		tabbedPane.addTab("Add New Book", addPanel);
		add(tabbedPane);
		setSize(490, 320);
		setLocationRelativeTo(null);
		setIconImage(new ImageIcon("images.jpg").getImage());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	public JTable createTable(ArrayList<Book> listOfResults) {
		JTable resultTable = new JTable();
		MyTableModel resultModel = new MyTableModel();
		resultModel.setColumnIdentifiers(columnNames);
		for (Book b : listOfResults) {
			Object[] bookInfo = { b.getIsbn(), b.getTitle(), b.getAuthor(),
					b.getReleaseDate(), b.isHardcover() ? "Yes" : "No",
					b.getNumberOfCopies() };
			resultModel.addRow(bookInfo);
		}
		resultTable.setModel(resultModel);
		return resultTable;

	}

	private class ListenerHandler implements ActionListener {
		String error = "Please enter a valid ";

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(searchSubmit)) {
				searchSubmitAction();
				
			} else if (e.getSource().equals(addSubmit)) {
				addSubmitAction();
			}
				
		}
		private void searchSubmitAction(){
			ArrayList<Book> resultList = germantownCatalog
					.searchTitle(searchField.getText());
			try {
				if (searchByIsbn.isSelected()) {
					if (searchField.getText().length() != 13
							&& searchField.getText().length() != 10) {
						throw new NumberFormatException();
					}
					resultList = germantownCatalog.searchISBN(Long
							.parseLong(searchField.getText()));
				} else if (searchByAuthor.isSelected()) {
					resultList = germantownCatalog.searchAuthor(searchField
							.getText());
				} else if (searchByReleaseDate.isSelected()) {
					if (searchField.getText().length() != 4) {
						throw new NumberFormatException();
					}
					resultList = germantownCatalog
							.searchReleaseDate(Integer.parseInt(searchField
									.getText()));
				}
			} catch (NumberFormatException ex) {

				if (searchByIsbn.isSelected()) {
					JOptionPane.showMessageDialog(root, error
							+ "10 or 13 digit ISBN number", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
				if (searchByReleaseDate.isSelected()) {
					JOptionPane.showMessageDialog(root, error
							+ "4 digit year", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
			}
			JTable resultTable = root.createTable(resultList);
			if (resultTable.getRowCount() <= 0) {
				JLabel none = new JLabel(
						"Sorry, there were no results for your search");
				tabbedPane.addTab("Search Results", none);
				tabbedPane.setSelectedIndex(2);
			} else {
				JScrollPane scrollPane = new JScrollPane(
						root.createTable(resultList));
				tabbedPane.addTab("Search Results", scrollPane);
				tabbedPane.setSelectedIndex(2);

			}
		}
		
		private void addSubmitAction(){
			if (!hardcoverField.getText().equalsIgnoreCase("Yes")
					&& !hardcoverField.getText().equalsIgnoreCase("No")) {
				JOptionPane.showMessageDialog(root,
						"Hardcover can only be yes or no", "Error",
						JOptionPane.ERROR_MESSAGE);
			}
			try {
				germantownCatalog
						.add(new Book(Long.parseLong(isbnField.getText()),
								titleField.getText(),
								authorField.getText(), Integer
										.parseInt(releaseField.getText()),
								hardcoverField.getText().equalsIgnoreCase(
										"Yes") ? true : false, Integer
										.parseInt(copiesField.getText())));
			} catch (NumberFormatException e1) {
				e1.printStackTrace();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			JOptionPane.showMessageDialog(root, "Your book has been added",
					"Notification", JOptionPane.INFORMATION_MESSAGE);
		}
		}

	public static void main(String[] args) {
		root = new CatalogUI();
	}

	private class MyTableModel extends DefaultTableModel {

		private static final long serialVersionUID = 1L;

		public boolean isCellEditable(int row, int column) {
			return false;
		}
	}
	
}
